package com.powroznik.tracker.liferay.portlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.util.bridges.mvc.MVCPortlet;
import com.powroznik.tracker.engine.services.Device;
import com.powroznik.tracker.engine.services.Location;

public class MapMVCPortlet extends MVCPortlet {
	@Override
	public void doView(RenderRequest req, RenderResponse res)
			throws IOException, PortletException {
		List<Device> devices = new ArrayList<Device>();
		Map<Device, Location> locations = new HashMap<Device, Location>();
		for (Device device : Client.getInstance(req).getConnectedDevices()) {
			Location location = Client.getInstance(req).getLastKnownLocation(
					device);
			if (location == null) {
				continue;
			}
			devices.add(device);
			locations.put(device, location);
		}

		req.setAttribute("devices", devices);
		req.setAttribute("locations", locations);

		super.doView(req, res);
	}
}
